package membertask

import (
	"context"

	"mall/common/resultx"
	"mall/service/backend/common/i18n"
	"mall/service/setting/rpc/internal/svc"
	"mall/service/setting/rpc/internal/utils/errorhandler"
	"mall/service/setting/rpc/types/setting"

	"github.com/zeromicro/go-zero/core/logx"
)

type BackendRestoreMemberTaskLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBackendRestoreMemberTaskLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BackendRestoreMemberTaskLogic {
	return &BackendRestoreMemberTaskLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// group: membertask
func (l *BackendRestoreMemberTaskLogic) BackendRestoreMemberTask(in *setting.IDsReq) (*setting.BaseResp, error) {
	tx, err := l.svcCtx.DB.BeginTx(l.ctx, nil)
	if err != nil {
		return nil, resultx.StatusError(resultx.DB_ERROR, "")
	}

	for _, id := range in.Ids {
		err = l.svcCtx.IntegralTask.Restore(l.ctx, tx, id)
		if err != nil {
			tx.Rollback()
			return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
		}
	}

	tx.Commit()

	return &setting.BaseResp{
		Msg: i18n.RestoreSuccess,
	}, nil
}
